package com.jiawang.core;

import com.jiawang.core.config.ServerConfig;
import com.jiawang.core.constant.SingleDigitConstant;
import com.jiawang.core.handler.Handler;
import com.jiawang.core.handler.RequestHandler;
import com.jiawang.core.log.LogUtils;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author LiJiaWang
 * 创建时间: 16:02
 * 项目的名字: jio_writer_tomcat
 * <p style="color:#33CCFF"></p>
 */

public class MySocketServer {
    private static LogUtils log =new LogUtils(MySocketServer.class.getCanonicalName());

    public static void main(String[] args) throws IOException {


    }


    public static class Run {
        public static void invoke() throws IOException {
            try (
                    ServerSocket serverSocket = new ServerSocket(ServerConfig.getInstance().getServerPort());
            ) {

                while (true) {
                    Socket socket = serverSocket.accept();
                    Handler requestHandler = new RequestHandler(socket);
                    Thread thread = new Thread(requestHandler);
                    thread.start();
                }
            }
        }
    }

    public static void run(Class<?> clazz) {
        try {


            Run.invoke();
        } catch (IOException e) {
            e.printStackTrace();
            log.server("初始化失败,异常信息为" + e.getMessage());
            System.exit(SingleDigitConstant.ONE);
        }
    }


}
